package com.qf.j2112.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.sql.Date;

public class JwtUtil {
    //    过期5分钟
    private static final long  EXPIRE_TIME = 5 * 60 * 1000 ;

    //    密钥
    private static final String  SECRET = "jwt_secret";

    /**
     * 生成签名，5分钟后过期
     * @param userId
     * @return
     */
    public static String sign(String userId){
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        Algorithm algorithm = Algorithm.HMAC256(SECRET);
        JWTCreator.Builder builder = JWT.create();
//        将userId写入到Token中
        builder.withAudience(userId);
//        设值token的生存期 5分钟后过期
        builder.withExpiresAt(date);
//       设值token的密钥
        String sign = builder.sign(algorithm);
//        String sign1 = JWT.create()
//                .withAudience(userId)
//                .withExpiresAt(date)
//                .sign(algorithm);

        return sign ;
    }

    /**
     * 从token中获取userId
     * @param token
     * @return
     */
    public static String  getUserId(String token){
        String userId = JWT.decode(token).getAudience().get(0);
        return userId;
    }
    /**
     * 校验token
     * @param token
     * @return
     */
    public static boolean checkToken(String token){
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET);
            JWTVerifier jwtVerifier = JWT.require(algorithm).build();
            DecodedJWT verify = jwtVerifier.verify(token);
            return true;
        }catch (JWTVerificationException ex){
            throw new RuntimeException("token expired");
        }

    }

}
